package com.cqggrw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.cqggrw.dao.FlashDaoI;
import com.cqggrw.domain.Flash;
import com.cqggrw.util.DBUtil;
import com.cqggrw.util.Utils;

public class FlashDaoImpl implements FlashDaoI {

	public boolean add(Flash flash) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "insert into tbl_flash(disPage, imgLink, imgUrl, name, chkTop) values(?, ?, ?, ?, ?);";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, flash.getDisPage());
			ps.setString(2, flash.getImgLink());
			ps.setString(3, flash.getImgUrl());
			ps.setString(4, flash.getName());
			ps.setInt(5, flash.getChkTop());
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public boolean del(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "delete from tbl_flash where id = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public boolean modify(Flash flash) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "update tbl_flash set disPage = ?, imgLink = ?, imgUrl = ?, name = ?, chkTop = ? WHERE ID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, flash.getDisPage());
			ps.setString(2, flash.getImgLink());
			ps.setString(3, flash.getImgUrl());
			ps.setString(4, flash.getName());
			ps.setInt(5, flash.getChkTop());
			ps.setInt(6, flash.getId());
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public Flash getById(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Flash f = null;
		
		String sql = "SELECT * FROM tbl_flash where id = ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();

			if (rs.next()) {
				f = new Flash();
				f.setId(rs.getInt("id"));
				f.setDisPage(rs.getInt("disPage"));
				f.setImgLink(rs.getString("imgLink"));
				f.setImgUrl(rs.getString("imgUrl"));
				f.setName(rs.getString("name"));
				f.setChkTop(rs.getInt("chkTop"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return f;
	}

	public List<Flash> get(Flash t) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Flash f = null;
		List<Flash> list = new ArrayList<Flash>();
		
		String sql = "select * from tbl_flash limit ?, ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (t.getPage()-1)*t.getRows());
			ps.setInt(2, t.getRows());
			rs = ps.executeQuery();

			while (rs.next()) {
				f = new Flash();
				f.setId(rs.getInt("id"));
				f.setDisPage(rs.getInt("disPage"));
				f.setImgLink(rs.getString("imgLink"));
				f.setImgUrl(rs.getString("imgUrl"));
				f.setName(rs.getString("name"));
				f.setChkTop(rs.getInt("chkTop"));
				list.add(f);
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

	public int getTotal() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int total = 0;
		
		String sql = "select count(*) from tbl_flash";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			if (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return total;
	}

	public List<Flash> getFlashImg(Integer i) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Flash f = null;
		List<Flash> list = new ArrayList<Flash>();
		
		String sql = "select * from (select * from tbl_flash where disPage = ? order by id desc limit 3) a order by chkTop desc;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, i);
			rs = ps.executeQuery();

			while (rs.next()) {
				f = new Flash();
				f.setId(rs.getInt("id"));
				f.setDisPage(rs.getInt("disPage"));
				f.setImgLink(rs.getString("imgLink"));
				f.setImgUrl(rs.getString("imgUrl"));
				f.setName(rs.getString("name"));
				f.setChkTop(rs.getInt("chkTop"));
				list.add(f);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

}
